<?php
function gcmsBanner_onLoad($db) {
	if (isset($_GET['id']) && isset($_GET['fetch'])) {
		$db->update('banners', array('clicks' => new Zend_Db_Expr('(clicks+1)')), 'id = ' . $db->quote($_GET['id']));
		header("Location: " . $db->fetchOne("SELECT url FROM `banners` WHERE id = ?", $_GET['id']));
		exit;
	}
	if ($GLOBALS['authNamespace']->admin && isset($_GET['save'])) {
		if (!empty($_GET['bid']))
			$db->update('banners', array('size' => $_GET['size'], 'image' => isset($_GET['image']) ? $_GET['image'] : '', 'url' => isset($_GET['url']) ? $_GET['url'] : '', 'code' => isset($_GET['code']) ? $_GET['code'] : '', 'active' => !empty($_GET['active']) ? 1 : 0), "id = " . $db->quote($_GET['bid']));
		else
			$db->insert('banners', array('client' => $_GET['client'], 'size' => $_GET['size'], 'image' => isset($_GET['image']) ? $_GET['image'] : '', 'url' => isset($_GET['url']) ? $_GET['url'] : '', 'code' => isset($_GET['code']) ? $_GET['code'] : ''));
	}
	elseif ($GLOBALS['authNamespace']->admin && isset($_GET['action']) && !empty($_GET['client'])) {
		if ($_GET['action'] == 'Add')
			$db->insert('clients', array('name' => $_GET['client']));
		elseif ($_GET['action'] == 'delete') {
			$db->delete('banners', 'client = ' . $db->quote($_GET['client']));  // Don't orphan banners
			$db->delete('clients', 'id = ' . $db->quote($_GET['client']));
		}
		else
			return;
		header("Location: index.php?mod=gcmsBanner&action=manage");
		exit;
	}
	elseif ($GLOBALS['authNamespace']->admin && isset($_GET['cancel'])) {
		header("Location: index.php?mod=gcmsBanner&action=" . $_GET['action'] . "&client=" . $_GET['client']);
		exit;
	}
}

function gcmsBanner_head($db, &$headers, $translate) {
	if ($_GET['action'] == 'new' || $_GET['action'] == 'edit')
		$headers->headScript()->appendFile("./modules/gcmsBanner/gcmsBanner.js");
}

function gcmsBanner_main($db, &$view, $translate) {
	if (!$GLOBALS['authNamespace']->admin)
		return;
	if (!empty($_GET['client']))
		$view->assign('client', $db->fetchOne("SELECT name FROM `clients` WHERE id = ?", $_GET['client']));
	switch ($_GET['action']) {
		case 'new':
		case 'edit':
			$view->assign('sizes', array('728x90', '468x60', '234x60', '120x600', '160x600', '120x240', '250x250', '200x200', '125x125'));
			$data = $db->fetchRow("SELECT * FROM `banners` WHERE id = ?", $_GET['bid']);
			$view->assign('data', $data);
			$view->display('file:../../../modules/gcmsBanner/edit.tpl');
			break;
		case 'delete':
			if (!empty($_GET['bid']))
				$db->delete('banners', 'id = ' . $db->quote($_GET['bid']));
		case 'client':
			$data = $db->query($db->select()->from('banners')->join('clients', 'clients.id = banners.client', array('name'))->where('banners.client = ?', $_GET['client']))->fetchAll();
			$view->assign('data', $data);
			$view->display('file:../../../modules/gcmsBanner/manage.tpl');
			break;
		default:
			$data = $db->fetchAll("SELECT * FROM `clients`");
			$view->assign('data', $data);
			$view->display('file:../../../modules/gcmsBanner/clients.tpl');
	}
}

function gcmsBanner_adminMenu($translate) {
	$stack = array();
	$stack[] = $translate->_("Banner Config");
	$stack['action=manage'] = $translate->_("Manage Banners");
	return $stack;
}

/**
 * Insert a random banner into the page.
 *
 * @param array $args Arguments to pass to the module in the form of an assoc array.
 */
function insert_gcmsBanner($args) {
	global $db;
	if (!isset($args['width']))
		$args['width'] = 468;
	if (!isset($args['height']))
		$args['height'] = 60;
	// Only select banners that match the correct size
	$banner = $db->fetchAll("SELECT * FROM `banners` WHERE size = '$args[width]x$args[height]' AND active = 1");
	if (count($banner) < 1)
		return;
	$rand = rand(0, count($banner) - 1);
	$db->update('banners', array('impressions' => new Zend_Db_Expr('(impressions+1)')), 'id = '.$banner[$rand]['id']);
	if (!empty($banner[$rand]['code']))
		echo $banner[$rand]['code']; 
	else
		echo "<a href=\"index.php?mod=banner&id=" . $banner[$rand]['id'] . "&url=" . $banner[$rand]['url'] . "\" target=\"_blank\"><img src=\"" . $banner[$rand]['image'] . "\" width=\"" . $args['width'] . "\" height=\"" . $args['height'] . "\" alt=\"Ad\" /></a>\n";
}
?>
